<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自定义redux</title>
</head>

<body>
    <button onclick="dispatch({ type: 'reduce', payload: 10 })">-</button>
    <div id="count"></div>
    <button onclick="dispatch({ type: 'add', payload: 10 })">+</button>
</body>
<script>
    // 设置初始状态
    const countState = {
        count: 10
    }
    // 定义一个方法用于渲染计算器的dom
    const renderCount = (state) => {
        const countDom = document.querySelector('#count')
        countDom.innerHTML = state.count
    }
    // 渲染默认数据
    renderCount(countState)

    // 分发
    function dispatch(action) {
        // 修改状态
        changeState(action)
        // 渲染数据
        renderCount(countState)
    }

    // 定义的纯函数
    function changeState(action) {
        const { type, payload } = action
        switch (type) {
            case 'add':
                countState.count += payload
                break
            case 'reduce':
                countState.count -= payload
                break
            default:
                countState
        }
    }
</script>

</html>